<?php
/**
 * Created by PhpStorm.
 * User: FoxxxZhu
 * Date: 2018/3/20
 * Time: 17:39
 */

namespace Forxx\Api;


use Lavender\Core;
use Lavender\Exception;

class FileCDN {
    CONST FILE_SAVE_REMOTE = "/file/save_remote_file";
    CONST FILE_UPLOAD = "/file/get_last_upload_list";
    public static function getFullUrlPath($path)
    {
        $baseUrl = Core::get_config("const","host")['upload'];
        return $baseUrl.$path;
    }

    public static function post($path, array $postData, $timeout = 30)
    {
        $url = self::getFullUrlPath($path);
        return Utils::httpJsonp($url,$postData,$timeout);
    }

    public static function saveRemoteImage($url,$file_type="jpg"){
        $path = self::FILE_SAVE_REMOTE;
        $postData = ['remote_url'=>$url,'file_type'=>$file_type];
        $data = self::post($path,$postData);
        if(is_array($data) && isset($data['code']) && $data['code'] == 0 && isset($data['data']))
        {
            return $data['data'];
        }
        else{
            if(defined('L_DEBUG')){
                Core::log("file_cdn_error","url:{$url} \n postData: ".json_encode($postData,JSON_UNESCAPED_UNICODE)."\n result:".json_encode($data,JSON_UNESCAPED_UNICODE)." \n\n");
            }
        }
        return null;
    }

    public static function upload($file_path,$file_type="jpg"){
        //#@todo
        throw new Exception("not Support");
    }
}